﻿/*
 * Author: .net follower (http://dotnetfollower.com)
 * Url: http://bdcrequestlib.codeplex.com/
 * Title: SharePoint Bdc Requests
 * Copyright: � 2012 .net follower 
 * Usage: software is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

using System.Collections.Generic;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.BusinessData.Infrastructure;

namespace SharePointBdcRequests
{
    /// <summary>
    /// Represents search of data item in external data source by basing on identifier(s)
    /// </summary>
    public class BdcRequestById : BdcRequestSingle
    {
        protected readonly Identity _requestedEntityInstanceIdentity;

        /// <summary>
        /// Encoded identifier(s) of data item
        /// </summary>
        public string RequestedEntityInstanceEncodedId
        {
            get { return ((BdcRequestByIdParams)_bdcRequestParams).EntityInstanceEncodedId; }
        }
        /// <summary>
        /// Array containing identifier(s) of data item
        /// </summary>
        public object[] RequestedEntityInstanceIds
        {
            get { return _requestedEntityInstanceIdentity.GetIdentifierValues(); }
        }

        public Identity RequestedEntityInstanceIdentity
        {
            get { return _requestedEntityInstanceIdentity; }
        }

        /// <summary>
        /// Initializes a new instance of the BDCRequestById class
        /// </summary>
        public BdcRequestById(BdcRequestByIdParams bdcRequestParams) : base(bdcRequestParams)
        {
            if (!string.IsNullOrEmpty(bdcRequestParams.EntityInstanceEncodedId) && EntityInstanceIdEncoder.IsEncodedIdentifier(bdcRequestParams.EntityInstanceEncodedId))
                // get decoded version of passed encoded identifier(s)
                _requestedEntityInstanceIdentity = new Identity(EntityInstanceIdEncoder.DecodeEntityInstanceId(bdcRequestParams.EntityInstanceEncodedId));
            else
                if (bdcRequestParams.EntityInstanceIds != null && bdcRequestParams.EntityInstanceIds.Length > 0)
                    // get encoded version of passed identifier(s)
                    _requestedEntityInstanceIdentity = new Identity(bdcRequestParams.EntityInstanceIds);

            EnsureParamIsNotNull("Instance Identity", _requestedEntityInstanceIdentity);
        }

        /// <summary>
        /// Gets BDC Entity Instance that meets identifier(s)
        /// </summary>
        /// <returns>Found BDC Entity Instance</returns>
        protected override List<IEntityInstance> GetEntityInstances()
        {
            List<IEntityInstance> res = new List<IEntityInstance>(1);

            // get BDC Entity Instance by its identifier(s)
            IEntityInstance instance = FoundEntity.FindSpecific(RequestedEntityInstanceIdentity, FoundLobSystemInstance);
            if(instance != null)
                res.Add(instance);

            return res;
        }
    }
}
